//
//  MineViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/8.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class MineViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {

    var source : Array<TarghModel> = []
    override func viewDidLoad() {
        super.viewDidLoad()
        self.naviHide = false
        self.title = "我的"
        self.view.backgroundColor = UIColor.colorWithHexString(hex: "f3f3f5")
        configTableView()
    }
    
    @objc func logOut()->Void{
        //请求登出接口,成功后将本地的缓存都删除,然后发送通知
        let account = DataCacher.shared().account()
        self.beginAnim()
        Manager.shared().logOut(account: account, success: {(model) in
            self.endAnim()
            if model.result == true {
                DataCacher.shared().removeAll()
                NotificationCenter.default.post(name: NSNotification.Name("needLogin"), object: nil)
            } else {
                self.commonAlertMsg(content: model.msg)
            }
        }, fail: {(code) in
            self.endAnim()
            self.commonAlertMsg(content: YXNetWorkFail)
        })
    }
    
    private func configTableView()->Void{
        let head = UIView.init()
        head.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: YXDeviceWidth, height: 240 * YXHeightScale))
        head.backgroundColor = UIColor.colorWithHexString(hex: "fefdfb")
        let imageView = UIImageView.init()
        imageView.frame = CGRect.init(origin: CGPoint.init(x: 56 * YXWidthScale, y: 50 * YXHeightScale), size: CGSize.init(width: 128 * YXWidthScale, height: 128 * YXHeightScale))
        imageView.layer.cornerRadius = YXButtonCorner  * 2
        imageView.layer.masksToBounds = true
        imageView.image = UIImage.load(name: "head_me_tel")
        head.addSubview(imageView)
        imageView.backgroundColor = UIColor.black
        let label = UILabel.init()
        let name = DataCacher.shared().name()
        let size = name.StringSize(font: UIFont.systemFont(ofSize: 32 * YXWidthScale))
        label.textColor = UIColor.colorWithHexString(hex: "2c2c2c")
        label.font = UIFont.systemFont(ofSize: 32 * YXWidthScale)
        label.text = name
        label.frame = CGRect.init(origin: CGPoint.init(x: 222 * YXWidthScale, y: 58 * YXHeightScale), size: size)
        let view = UIView.init()
        view.frame = CGRect.init(origin: CGPoint.init(x: 0, y: head.bottom() - 20 * YXHeightScale), size: CGSize.init(width: YXDeviceWidth, height: 20 * YXHeightScale))
        view.backgroundColor = UIColor.colorWithHexString(hex: "f3f3f5")
        head.addSubview(view)
        head.addSubview(label)
        let account = "账户:" + DataCacher.shared().account()
        let size1 = account.StringSize(font: UIFont.systemFont(ofSize: 32 * YXWidthScale))
        let label1 = UILabel.init()
        label1.textColor = UIColor.colorWithHexString(hex: "2c2c2c")
        label1.font = UIFont.systemFont(ofSize: 32 * YXWidthScale)
        label1.text = account
        label1.frame = CGRect.init(origin: CGPoint.init(x: label.left(), y: label.bottom() + 38 * YXHeightScale), size: size1)
        head.addSubview(label1)
        let table = UITableView.init(frame: self.view.bounds, style: .plain)
        table.delegate = self
        table.dataSource = self
        table.register(MinePocketCell.classForCoder(), forCellReuseIdentifier: "monthData")
        table.separatorStyle = .singleLine
        table.separatorInset = UIEdgeInsetsMake(0, 100 * YXWidthScale, 0, 0)
        table.layoutMargins = UIEdgeInsetsMake(0, 100 * YXWidthScale, 0, 0)
        
        table.tableHeaderView = head
        self.view.addSubview(table)
        let mod1 = TarghModel.init()
        mod1.image = "icon_me_money"
        mod1.title = "我的钱包"
        let mod2 = TarghModel.init()
        mod2.image = "icon_me_news"
        mod2.title = "我的消息"
        let mod3 = TarghModel.init()
        mod3.image = "icon_me_about"
        mod3.title = "关于我们"
        source.append(mod1)
        source.append(mod2)
        source.append(mod3)
        let footer = UIView.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: YXDeviceWidth, height: YXDeviceHeight - 640 * YXHeightScale - 113)))
        footer.backgroundColor = UIColor.white
        let line = UIView.init()
        line.frame = CGRect.init(origin: CGPoint.init(x: 100 * YXWidthScale, y: -YXHeightScale), size: CGSize.init(width: YXDeviceWidth, height: YXHeightScale))
        line.backgroundColor = table.separatorColor
        footer.addSubview(line)
        
        let button = UIButton.init(type: .system)
        button.setTitle("退出登录", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 32 * YXWidthScale)
        button.setTitleColor(UIColor.colorWithHexString(hex: "2c2c2c"), for: .normal)
        button.layer.cornerRadius = YXButtonCorner
        button.layer.borderColor = UIColor.colorWithHexString(hex: "c1c1c1").cgColor
        button.layer.borderWidth = YXHeightScale
        button.frame = CGRect.init(origin: CGPoint.init(x: YXDeviceWidth / 2 - 275 * YXWidthScale, y: footer.height() - 148 * YXHeightScale), size: CGSize.init(width: 550 * YXWidthScale, height: 88 * YXHeightScale))
        button.addTarget(self, action: #selector(logOut), for: .touchUpInside)
        footer.addSubview(button)
        table.tableFooterView = footer
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "monthData")! as! MinePocketCell
        cell.model = source[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100 * YXHeightScale
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.separatorInset = UIEdgeInsetsMake(0, 100 * YXWidthScale, 0, 0)
        cell.layoutMargins = UIEdgeInsetsMake(0, 100 * YXWidthScale, 0, 0)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.row == 0 {
            let pocket = PocketViewController.init()
            pocket.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(pocket, animated: true)
        }else if indexPath.row == 1 {
            let message = MyMessageViewController.init()
            message.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(message, animated: true)
        }else if indexPath.row == 2 {
            let aboutUs = AboutUsViewController.init()
            aboutUs.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(aboutUs, animated: true)
        }
    }

}
